<!-- 公共文件 start -->
@extends('admin.public.base')
<!-- 公共文件 end -->

@section('content')
    <div class="layui-fluid">
        <div class="layui-row">
            <form class="layui-form">
                <div class="layui-form-item" style="text-align:center;font-size:18px;">
                    <b>出货单号：{{$data['order_sn']}}</b>
                    <a href="/admin/order/orderPdf?id={{$data['id']}}">
                        <div class="icon iconfont" style="float:right;">&#xe6c9;</div>
                    </a>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label class="layui-form-label"><b>代理商信息：</b></label>
                    <div class="layui-input-block" style="line-height:36px">
                        {{$storeInfo['store_name']}}<br>
                        货款余额：{{$storeInfo['money_goods']}}元<br>
                        管理费余额：{{$storeInfo['money_management']}}元 <br>
                        联系人：{{$storeInfo['principal_name']}}，手机号：{{$storeInfo['principal_mobile']}}
                        ，地址：{{$storeInfo['principal_address']}}
                    </div>
                </div>
                <hr>
                <div class="layui-card-body layui-table-body layui-table-main">
                    <b>已选商品</b><br>
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th>图片</th>
                            <th>商品名</th>
                            <th>商品分类</th>
                            <th>商品编号</th>
                            <th>是否赠品</th>
                            <th>单件价格</th>
                            <th>出库数量</th>
                            <th>实付价格</th>
                            <th>入库批次</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach( $goodsList as $kk => $item)
                            <tr>
                                <td><img src="{{ $item['goods_img'] }}" height="40px" alt="品牌图"></td>
                                <td>{{ $item['goods_name'] }}</td>
                                <td>{{ $item['cate_name'] }}</td>
                                <td>{{ $item['goods_sn'] }}</td>
                                <td>@if($item['is_give'] == 1) <b style="color:#F03752">是</b> @else <b>否</b> @endif
                                </td>
                                <td>{{ $item['goods_price'] }}</td>
                                <td>{{ $item['goods_number'] }}</td>
                                <td>{{ $item['goods_amount'] }}</td>
                                <td>
                                    @if(isset($item['store_product_batch']) && $item['store_product_batch'])
                                        {{ $item['store_product_batch'] }}
                                    @else
                                        <select name="goods[{{$loop->index}}][batch]" class="form-control batch-select" required>
                                            <option value="">请选择批次</option>
                                            @foreach($goodsBatchInfo[$item['goods_id']] ?? [] as $batch)
                                                <option value="{{ $batch }}" @if(isset($item['store_product_batch']) && $item['store_product_batch'] == $batch) selected @endif>{{ $batch }}</option>
                                            @endforeach
                                        </select>
                                        <input type="hidden" name="goods[{{$loop->index}}][goods_id]" value="{{$item['goods_id']}}">
                                    @endif
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label class="layui-form-label"><b>收货信息：</b></label>
                    <div class="layui-input-block" style="line-height:36px">
                        收货人：{{$data['order_name']}}<br/>
                        手机号：{{$data['order_mobile']}}<br/>
                        地址：{{ $data['order_province'] }}，{{ $data['order_city'] }}，{{ $data['order_area'] }}
                        ，{{$data['order_address']}}<br/>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label"><span class="x-red">*</span>发货单号</label>
                        <div class="layui-input-inline">
                            <input type="text" name="tracking_number" value="{{ $data['tracking_number'] }}"
                                   class="layui-input">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">发货快递</label>
                        <div class="layui-input-inline" style="line-height:36px">
                            {{ $data['express_company'] ?? '' }}
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-block">
                        <label class="layui-form-label">备注</label>
                        <div class="layui-input-block">
                            <textarea placeholder="请输入内容" class="layui-textarea"
                                      name="remark">{{ $data['remark'] }}</textarea>
                        </div>
                    </div>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label class="layui-form-label"><b>订单状态：</b></label>
                    <div class="layui-input-block" style="line-height:36px">
                        订单状态：<b style="color:{{$data['order_status_color']}}">{{$data['order_status_name']}}</b><br>
                        发货状态：<b style="color:{{$data['send_status_color']}}">{{$data['send_status_name']}}</b><br>
                        操作人：{{$data['update_user']}}
                    </div>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label class="layui-form-label"><b>总计：</b></label>
                    <div class="layui-input-block" style="line-height:36px">
                        数量：{{$data['total_number']}}个， 运费：{{$data['freight']}}元， 金额：{{$data['goods_pay_amount']}}元，
                        总金额：{{$data['order_amount']}}元 <br>
                    </div>
                </div>
                <hr>
                <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label"></label>
                    <input type="hidden" value="{{ $data['id'] }}" name="id">
                </div>
            </form>
        </div>
    </div>
    <script type="text/javascript" src="/static/admin/js/xcity.js"></script>
    <script>
        layui.use(['form', 'layer', 'jquery', 'code'], function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            // 初始化批次选择
            $(document).ready(function() {
                // 初始化选择第一个批次
                $('.batch-select').each(function() {
                    if ($(this).find('option').length > 1) {
                        $(this).val($(this).find('option:eq(1)').val());
                    }
                });
                
                // 表单提交前验证
                $('form').on('submit', function(e) {
                    let valid = true;
                    $('.batch-select').each(function() {
                        if (!$(this).val()) {
                            layer.alert('请为所有商品选择入库批次');
                            valid = false;
                            return false;
                        }
                    });
                    return valid;
                });
            });

            //监听提交
            form.on('submit(add)', function (data) {
                // 验证批次选择
                let valid = true;
                $('.batch-select').each(function() {
                    if (!$(this).val()) {
                        layer.alert('请为所有商品选择入库批次');
                        valid = false;
                        return false;
                    }
                });
                if (!valid) {
                    return false;
                }

                if (confirm("你确定通过-转出库单吗？")) {
                    //发异步，把数据提交给php
                    $.ajax({
                        type: 'POST',
                        url: '/admin/order/orderEdit',
                        data: data.field,
                        dataType: 'json',
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                        },
                        success: function (data) {
                            layer.alert(data.msg)
                            if (data.code == 1) {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            }
                        },
                        error: function (xhr, type) {
                            alert('Ajax error!')
                        }
                    });
                }
                return false;
            });
            //监听提交
            form.on('submit(cancel)', function (data) {
                //发异步，把数据提交给php
                if (confirm("你确定审核不通过吗？")) {
                    $.ajax({
                        type: 'POST',
                        url: '/admin/order/orderEdit?cancel=1',
                        data: data.field,
                        dataType: 'json',
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
                        },
                        success: function (data) {
                            layer.alert(data.msg)
                            if (data.code == 1) {
                                //关闭当前frame
                                xadmin.close();
                                // 可以对父窗口进行刷新
                                xadmin.father_reload();
                            }
                        },
                        error: function (xhr, type) {
                            alert('Ajax error!')
                        }
                    });
                }
                return false;
            });
        });
    </script>
@endsection
